package com.zlc.big_event.controller;

import com.zlc.big_event.pojo.Article;
import com.zlc.big_event.pojo.PageBean;
import com.zlc.big_event.pojo.Result;
import com.zlc.big_event.service.ArticleService;
import org.apache.coyote.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;
    // 发布文章
    @PostMapping
    public Result add(@RequestBody @Validated Article article){
        // 调用服务层的方法来添加新的文章
        articleService.add( article );
        // 返回操作成功的结果
        return Result.success();
    }

    //文章列表(条件分页)
    @GetMapping
    public Result<PageBean<Article>> list(Integer pageNum,
                                          Integer pageSize,
                                          @RequestParam(required = false) String categoryId,
                                          @RequestParam(required = false) String state){
        PageBean<Article> pb = articleService.list( pageNum,pageSize,categoryId,state );
        return Result.success( pb );

    }

    //更新文章
    @PutMapping
    public Result update(@RequestBody Article article){
        //调用文章服务的更新方法，传入文章对象
        articleService.update(article);
        //返回成功结果
        return Result.success();
    }

    //获取文章详情
    @GetMapping("/detail")
    public Result<Article> detail(Integer id){
        return Result.success(articleService.FindById(id));
    }

    //删除文章
    @DeleteMapping
    public Result delete( Integer id){
        articleService.delete(id);
        return Result.success();
    }


}
